<?php
namespace app\center\controller;
use app\center\logic\WaitMatterUserLogic;

/**
 * 待办事项个人api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/08
 * Time: 14:46
 */
class WaitMatterUser extends Base {

    public function __construct(){
        parent::__construct();
        $this->wmu_logic = new WaitMatterUserLogic();
    }

    /**
     * 获取待办事项个人
     * @return \think\response\Json
     */
    public function ajax_get_wmu(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->wmu_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        $maps = $this->wmu_logic->get_search_wmu_maps($u_detail['openid'],$params['from_openid'],$params['source'],$params['start_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('matter')->get_list($maps,'id,from_openid,source,name,out_time,is_read,c_time','is_read asc,id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['from_openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username');

            $content = '发起人：' . $u_detail['username'];
            $content = ($v['out_time'] != '0000-00-00 00:00:00') ? $content . '，截止时间：' . $v['out_time'] : $content;

            $list[$k]['content'] = $content;
        }

        $total_num = model('matter')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        $data['page'] = $params['page'];
        return json($this->wmu_logic->get_response(1,'OK',$data));
    }

    /******************************************************巡店**********************************************************/
    /**
     * 处理待办事项个人（巡店）
     * @return \think\response\Json
     */
    public function ajax_deal_wmu_cp()
    {
        $params = input();
        $r = $this->check_user_token($params);
        if ($r['code'] != 1) {
            return json($r);
        }

        //检查用户规则
        $r = $this->wmu_logic->check_user_rule();
        if ($r['code'] != 1) {
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查处理待办事项个人规则（巡店）
        $r = $this->wmu_logic->check_deal_wmu_cp_rule($u_detail['openid'],$params);
        if (!$r['code']) {
            return json($r);
        }
        $m_detail = $r['data']['m_detail'];

        //更新事项为已读
        if($u_detail['openid'] == $m_detail['deal_openid']){
            $r = $this->wmu_logic->save_matter_read($m_detail['id']);
        }

        //生成事项流程
        $m = new \app\common\controller\Matter();
        $r = $m->gen_matter_flow($u_detail['openid'],$m_detail['id'],$params['action'],$params['out_time'],$params['deal_openid'],$params['explain'],$params['pic']);

        //生成抄送
        $c = new \app\common\controller\Copy();
        if(!empty($params['copy_openid'])){
            //生成抄送（未抄送）
            $r = $c->gen_copy_not($u_detail['openid'],$m_detail['sourceid'],1,$params['copy_openid']);
            //修改抄送（已抄送）
            $r = $c->save_copy_open($u_detail['openid'],$m_detail['sourceid'],1);
        }

        return json($this->wmu_logic->get_response(1,'OK'));
    }
    /******************************************************巡店**********************************************************/

    /****************************************************内部协同********************************************************/
    /**
     * 处理待办事项个人（协同）
     * @return \think\response\Json
     */
    public function ajax_deal_wmu_xt(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->wmu_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查处理待办事项个人规则（协同）
        $r = $this->wmu_logic->check_deal_wmu_xt_rule($u_detail['openid'],$params);
        if(!$r['code']){
            return json($r);
        }
        $m_detail = $r['data']['m_detail'];

        //更新事项为已读
        if($u_detail['openid'] == $m_detail['deal_openid']){
            $r = $this->wmu_logic->save_matter_read($m_detail['id']);
        }

        //生成事项流程
        $m = new \app\common\controller\Matter();
        $r = $m->gen_matter_flow($u_detail['openid'],$m_detail['id'],$params['action'],$params['out_time'],$params['deal_openid'],$params['explain'],$params['pic']);

        //生成抄送
        $c = new \app\common\controller\Copy();
        if(!empty($params['copy_openid'])){
            //生成抄送（未抄送）
            $r = $c->gen_copy_not($u_detail['openid'],$m_detail['sourceid'],2,$params['copy_openid']);
            //修改抄送（已抄送）
            $r = $c->save_copy_open($u_detail['openid'],$m_detail['sourceid'],2);
        }
        return json($this->wmu_logic->get_response(1,'OK'));
    }
    /******************************************************内部协同*******************************************************/
}
